package com.yuexi.app.syscode;

import com.yuexi.app.coverter.Converter;
import com.yuexi.domain.syscode.SysCodeDomain;
import com.yuexi.domain.syscode.bo.SysCodeQueryBO;
import com.yuexi.domain.syscode.bo.SysCodeResultBO;
import com.yuexi.syscode.SysCodeService;
import com.yuexi.syscode.cqe.DropListQuery;
import com.yuexi.syscode.cqe.SysCodeQuery;
import com.yuexi.syscode.dto.SysCodeDTO;
import com.zenx.dto.TitleValueDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author jie
 * @date 2021/12/15 -10:11
 */
@DubboService
@Service
public class SysCodeServiceImpl implements SysCodeService {

    @Resource
    private SysCodeDomain sysCodeDomain;

    @Override
    public List<SysCodeDTO> query(SysCodeQuery sysCodeQuery) {
        SysCodeQueryBO sysCodeQueryBO = Converter.INSTANCE.toSysCodeQueryBO(sysCodeQuery);
        return Converter.INSTANCE.toSysCodeDTOList(sysCodeDomain.query(sysCodeQueryBO));
    }

    @Override
    public SysCodeDTO getOne(SysCodeQuery sysCodeQuery) {
        SysCodeQueryBO sysCodeQueryBO = Converter.INSTANCE.toSysCodeQueryBO(sysCodeQuery);
        return  Converter.INSTANCE.toSysCodeDTO(sysCodeDomain.getOne(sysCodeQueryBO));
    }

    @Override
    public List<SysCodeDTO> getList(String code) {
        List<SysCodeResultBO> sysCodeList = sysCodeDomain.list(code);
        return Converter.INSTANCE.toSysCodeDTOList(sysCodeList);
    }

    @Override
    public List<TitleValueDTO> dropList(DropListQuery dropListQuery) {
        List<TitleValueDTO> titleValueDTOList = new ArrayList<>();
        List<SysCodeResultBO> sysCodeList = sysCodeDomain.list(dropListQuery.getCode());
        sysCodeList.forEach(sysCode -> {
            TitleValueDTO titleValueDTO = new TitleValueDTO();
            titleValueDTO.setTitle(sysCode.getNameCn());
            titleValueDTO.setValue(sysCode.getSubCode());
            titleValueDTOList.add(titleValueDTO);
        });
        return titleValueDTOList;
    }

    @Override
    public Map<String, List<TitleValueDTO>> dropListBatch(Set<String> codeIds) {
        Map<String, List<TitleValueDTO>> map = new HashMap<>();
        if (CollectionUtils.isEmpty(codeIds)) {
           return map;
        }

        List<SysCodeResultBO> sysCodeResultBOS = sysCodeDomain.batchQuery(codeIds);
        for (String codeId : codeIds) {
            List<TitleValueDTO> titleValueDTOList = new ArrayList<>();
            for (SysCodeResultBO sysCode : sysCodeResultBOS) {
                if (codeId.equals(sysCode.getCode())) {
                    TitleValueDTO titleValueDTO = new TitleValueDTO();
                    titleValueDTO.setTitle(sysCode.getNameCn());
                    titleValueDTO.setValue(sysCode.getSubCode());
                    titleValueDTOList.add(titleValueDTO);
                }
            }
            map.put(codeId, titleValueDTOList);
        }
        return map;
    }
}
